//
//  VisaHelperViewController.m
//  MyTravel
//
//  Created by Hunk on 11-6-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "VisaHelperViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "NetworkManager.h"
#import "MyLoadingView.h"

#define TAG_ANSWERTEXTVIEW (121)

@implementation VisaHelperViewController
@synthesize visaHelperDictionary;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    SAFE_RELEASE(visaHelperDictionary)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"签证问题解答";
    
    // Background view
    UIImageView* bgImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)] autorelease];
    [bgImageView setImage:[[UIImage imageNamed:@"CellBackground.png"]
                           stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
    [self.view addSubview:bgImageView];
    
    // Question label
    UILabel* questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 310, 48)];
    [questionLabel setBackgroundColor:[UIColor clearColor]];
    [questionLabel setFont:[UIFont fontWithName:@"AmericanTypewriter" size:18]];
    [questionLabel setTextColor:[UIColor blueColor]];
    [questionLabel setTextAlignment:UITextAlignmentCenter];
    [questionLabel setLineBreakMode:UILineBreakModeCharacterWrap];
    questionLabel.numberOfLines = 2;
    [questionLabel setText:[visaHelperDictionary objectForKey:@"question"]];
    [bgImageView addSubview:questionLabel];
    [questionLabel release];
    
    // Bg view
    UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(2, 50, 316, 361)];
    [bgView setBackgroundColor:[UIColor clearColor]];
    bgView.layer.borderWidth = 1.0;
    bgView.layer.borderColor = [UIColor brownColor].CGColor;
    [self.view addSubview:bgView];
    [bgView release];
    
    // Answer textview
    UITextView* answerTextView = [[UITextView alloc] initWithFrame:CGRectMake(8, 50, 325, 360)];
    [answerTextView setBackgroundColor:[UIColor clearColor]];
    [answerTextView setShowsVerticalScrollIndicator:NO];
    [answerTextView setFont:[UIFont fontWithName:@"AmericanTypewriter" size:18]];
    [answerTextView setTextAlignment:UITextAlignmentLeft];
    [answerTextView setEditable:NO];
    [answerTextView setText:[visaHelperDictionary objectForKey:@"visaAnswers"]];
    [answerTextView setTag:TAG_ANSWERTEXTVIEW];
    [self.view addSubview:answerTextView];
    [answerTextView release];
    
    // Data
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
    
    // NetworkRequest_GetVisaQuestions
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetVisaAnswers
                                                   WithParameters:[NSDictionary dictionaryWithObjectsAndKeys:[visaHelperDictionary objectForKey:@"id"], @"question_id", nil]
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
}

#pragma mark -
#pragma mark GetVisaAnswers
- (void)onSuccess:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
    UITextView* answerTextView = (UITextView*)[self.view viewWithTag:TAG_ANSWERTEXTVIEW];
    
    [answerTextView setText:[resultDic objectForKey:@"visaAnswer"]];
}

- (void)onFailed:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
    ALERT_GENERAL([resultDic objectForKey:@"message"])
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
